var wxCharts = require('../../../utils/wxcharts.js');
const app = getApp();
var lineChart = null;
var startPos = null;
var sliderWidth = 115; // 需要设置slider的宽度，用于计算中间位置 iphone6 是100 6P是 115
Page({
  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    target: 0,
    reportType: 'week',
    tabs: ['餐前血糖','餐后血糖','尿酸','胆固醇','甘油三酯'],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    next:0
  },
  touchHandler: function (e) {
    lineChart.scrollStart(e);
  },
  moveHandler: function (e) {
    lineChart.scroll(e);
  },
  touchEndHandler: function (e) {
    lineChart.scrollEnd(e);
    lineChart.showToolTip(e, {
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    self.setData({
      auth: app.globalData.auth
    });
    var activeIndex = options.type != 0 ? parseInt(options.type) + 1 : options.type;
    this.setData({
      activeIndex: activeIndex
    });
    wx.getSystemInfo({
      success: function (res) {
        self.setData({
          sliderLeft: -(res.windowWidth / self.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / self.data.tabs.length * self.data.activeIndex
        });
      }
    });
    this.preInitCharts(0);
  },

  //明细列表
  loadCountData: function (type,next,cqch) {
    var self = this;
    var threeSession = app.get3Session();
    wx.request({
      url: app.globalData.domain + 'test/mx/list?threeSession=' + threeSession + "&type=" + type+"&next="+next+"&cqch="+cqch,
      dataType: 'json',
      success: function (data) {
        if(data.data=='fail'){
          return
        }
        self.setData({
          listData: data.data,
        });
      }
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var self = this;
    var threeSession = app.get3Session();
    var type = (self.data.activeIndex == 0 || self.data.activeIndex == 1) ? 0 : self.data.activeIndex - 1;
    var cqch = (self.data.activeIndex == 0 || self.data.activeIndex == 1) ? self.data.activeIndex : 0;
    var next = self.data.next + 10;
    wx.request({
      url: app.globalData.domain + 'test/mx/list?threeSession=' + threeSession + "&type=" + type + "&next=" + next + "&cqch=" + cqch,
      dataType: 'json',
      success: function (data) {
        if (data.data == 'fail') {
          return
        }
        var current = self.data.next+10;
        var arr = self.data.listData;
        if (data.data != null && data.data.length >0){
          for (var i = 0; i < data.data.length;i++){
            arr.push(data.data[i]);
          }
        }
        self.setData({
          listData: arr,
          next: current
        });
      }
    });
  },
  preInitCharts:function(cqch){
    var self = this;
    var threeSession = app.get3Session();
    var type = (self.data.activeIndex == 0 || self.data.activeIndex == 1) ? 0 : self.data.activeIndex-1;
    wx.request({
      url: app.globalData.domain + 'test/mx/report?threeSession=' + threeSession + "&type=" + type + "&cqch="+cqch,
      dataType: 'json',
      success: function (data) {
        if (data.data == 'fail') {
          return
        }
        var type = (self.data.activeIndex == 0 || self.data.activeIndex == 1) ? 0 : self.data.activeIndex - 1;
        var name = (type == 0&&cqch==0) ? '餐前血糖' : (type==0&&cqch==1) ? '餐后血糖' : type == 1 ? '尿酸':type == 2 ? '胆固醇':type==3? '甘油三酯':'';
        self.initCharts(data.data, name);
        var cqch = (self.data.activeIndex == 0 || self.data.activeIndex == 1) ? self.data.activeIndex : 0;
        var type = (self.data.activeIndex == 0 || self.data.activeIndex == 1) ? 0 : self.data.activeIndex -1
        self.loadCountData(type, 0, cqch);
      }
    });
  },

  initCharts: function (options,name) {
    if (options.categories.length == 0 || options.data.length == 0 || options.target.length ==0){
      return;
    }
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
    }
    var id = 'areaCanvas' + this.data.activeIndex;

    lineChart = new wxCharts({
      canvasId: id,
      type: 'line',
      categories: options.categories,
      animation: false,
      series: [{
        name: name+'目标值',
        data: options.target,
        format: function (val) {
          return val;
        }
      }, {
        name: name+'实际值',
        data: options.data,
        format: function (val) {
          return val;
        }
      },
      ],
      yAxis: {
        title: 'mmol/L',
        format: function (val) {
          return val;
        },
        min: 0,
        fontColor: '#8085e9',
        gridColor: '#FFFFFF',
        titleFontColor: '#f7a35c',
        disabled: true
      },
      xAxis: {
        disableGrid: false,

      },
      dataLabel: false,
      extra: {
        legendTextColor: '#cb2431'
      },
      width: windowWidth,
      height: 250,
      dataLabel: true,
      dataPointShape: true,
      enableScroll: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  },
  tabClick: function (e) {
    var self = this;
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    if (e.currentTarget.id == 0 || e.currentTarget.id == 1){
      self.preInitCharts(e.currentTarget.id);
    }else{
      self.preInitCharts(0);
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  
  onShareAppMessage: function (ops) {
   
  },
})